package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.ImageDriver;
import com.bocloud.cmp.driver.providers.ali.AliImageDriver;
import com.bocloud.cmp.driver.providers.intf.ImageProvider;
import com.bocloud.cmp.driver.providers.jd.JdImageDriver;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.providers.os.OSImageDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.ImageModel;
import com.bocloud.common.model.BsmResult;

/**
 * @author dmw
 *
 */
@Service("imageProvider")
public class ImageProviderImpl implements ImageProvider {

	@Override
	public BsmResult list(String vendor, String region) {
		ImageDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdImageDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			break;
		case ALIYUN:
			driver = new AliImageDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			break;
		case OPENSTACK:
			driver = new OSImageDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.list();
		}
	}

	@Override
	public BsmResult detail(String imageId, String vendor, String region) {
		BsmResult result = null;
		ImageDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			driver = new JdImageDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
			result = driver.detail(imageId);
			break;
		case ALIYUN:
			driver = new AliImageDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.detail(imageId);
			break;
		case OPENSTACK:
			driver = new OSImageDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.detail(imageId);
			break;
		default:
			break;
		}
		return result;
	}

	@Override
	public BsmResult remove(String id, String vendor, String region, String vmName) {
		Butler butler = Configuration.instance().getConfig(vendor);
		ImageDriver driver = null;
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case OPENSTACK:
			driver = new OSImageDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			if (type.name().equals(VendorType.OPENSTACK.name())) {
				return driver.remove(id);
			}
		}
		return null;
	}

	@Override
	public BsmResult modify(String vendor, String region, ImageModel image) {
		BsmResult result = null;
		ImageDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case JDYUN:
			break;
		case ALIYUN:
			break;
		case OPENSTACK:
			driver = new OSImageDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			result = driver.modify(image);
			break;
		case VMWARE:
			break;
		default:
			result = new BsmResult(false, "NOT_SUPPORTED");
			break;
		}
		return result;
	}

}
